package com.bjpowernode.dataservice.service;

import com.bjpowernode.api.dto.UserAccountInfo;
import com.bjpowernode.api.dto.UserRechargeInfo;
import com.bjpowernode.api.model.FinanceAccount;
import com.bjpowernode.api.model.User;
import com.bjpowernode.api.serivce.UserService;
import com.bjpowernode.dataservice.mapper.FinanceAccountMapper;
import com.bjpowernode.dataservice.mapper.UsersMapper;
import com.bjpowernode.util.CommonUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author 麦翔盛
 * @ClassName: UserServiceImpl
 * @Package: com.bjpowernode.dataservice.service
 * @description: 用户 逻辑层 实现类
 * @date 2022/5/21 16:54
 */
@Service

@DubboService(interfaceClass = UserService.class, version = "1.0")
public class UserServiceImpl implements UserService {

    @Resource
    private UsersMapper usersMapper;

    @Resource
    private FinanceAccountMapper financeAccountMapper;

    @Value(value = "${reg.user.salt}")
    private String salt;

    /**
     * 注册用户
     *
     * @param phone    电话号码
     * @param password 密码
     * @return 0参数错误 1注册成功 2该用户已经存在
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public synchronized Integer userRegister(String phone, String password) {
        int regResult = 0;
        //检查数据格式是否存在
        if (CommonUtils.checkPhone(phone) && CommonUtils.checkPassword(password)) {
            //检查这个用户是否已经存在
            User checkUser = usersMapper.selectByPhoneUser(phone);
            if (checkUser != null) {
                //如果这个用户已经存在了就返回2
                return 2;
            }
            User regUser = new User();
            regUser.setAddTime(new Date());
            regUser.setPhone(phone);

            //加密
            String newPassword = DigestUtils.md5Hex(password + salt);

            regUser.setLoginPassword(newPassword);

            //插入数据 用户表中
            usersMapper.insertSelectId(regUser);


            System.out.println("regUser.getId() = " + regUser.getId());

            //新建用户账户

            FinanceAccount newAccount = new FinanceAccount();
            newAccount.setAvailableMoney(new BigDecimal("0"));
            newAccount.setUid(regUser.getId());
            financeAccountMapper.insertSelective(newAccount);


            //表示注册成功
            regResult = 1;
        }
        return regResult;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized User userLogin(String phone, String originPassword) {
        //校验数据格式
        if (!CommonUtils.checkPhone(phone) || !CommonUtils.checkPassword(originPassword)) {
            return null;
        }
        //获取用户
        String newPassword = DigestUtils.md5Hex(originPassword + salt);


        return usersMapper.selectUserByPhoneAndPassword(phone, newPassword);
    }


    @Override
    public boolean userExist(String phone) {
        return usersMapper.selectByPhoneUser(phone) != null;
    }

    @Override
    public boolean isVerifiedRealName(Integer uid) {
        boolean flag = false;
        User user = usersMapper.selectByPrimaryKey(uid);
        //姓名和身份证都不为空
        if (user != null && StringUtils.isNoneBlank(user.getName(), user.getIdCard())) {
            flag = true;
        }
        return flag;
    }


    @Override
    public void updateUserRealInfo(Integer uid, String name, String idNo) {
        //创建实体类
        User user = new User();
        user.setId(uid);
        user.setName(name);
        user.setIdCard(idNo);
        //更新不为null的值
        usersMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public User queryUserById(Integer uid) {
        return usersMapper.selectByPrimaryKey(uid);
    }

    @Override
    public UserAccountInfo queryUserAccountInfoById(Integer uid) {
        if (uid != null && uid > 0) {
            return usersMapper.selectUserAccountInfoById(uid);
        }
        return null;
    }

    @Override
    public List<UserRechargeInfo> queryUserRechargeInfoByUid(Integer uid, Integer pageNo, Integer pageSize) {
        if (uid != null && uid > 0) {
            int limit = pageSize;
            int offset = (pageNo - 1) * limit;
            return usersMapper.selectUserRechargeInfoById(uid, offset, limit);
        }
        return null;
    }
}










